package com.sk.sourcecircle.activity.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.ChatActivity;
import com.sk.sourcecircle.activity.home.activity.GroupDetailsActivity;
import com.sk.sourcecircle.activity.login.LoginActivity;
import com.sk.sourcecircle.adapter.GroupCircleAdapter;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.dialog.TextTipDialogFragment;
import com.sk.sourcecircle.entity.ApplyBean;
import com.sk.sourcecircle.entity.GroupCircleList;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.sk.sourcecircle.activity.home.activity.GroupDetailsActivity.Group;

/**
 * 1
 * Created by sk on 2017/12/13.
 */

public class GroupCircleFragment extends BaseFragment implements HandleDataCallBack {

    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.more_public)
    RelativeLayout morePublic;
    @BindView(R.id.public_ryc)
    RecyclerView publicRyc;
    @BindView(R.id.line3)
    View line3;
    @BindView(R.id.more_interst)
    RelativeLayout moreInterst;
    @BindView(R.id.interst_ryc)
    RecyclerView interstRyc;
    Unbinder unbinder;

    private GroupCircleAdapter adapter1, adapter2;

    private int id;
    private LoadingDialog loadingDialog;
    private List<GroupCircleList.DataBean> publicList = new ArrayList<>();
    private List<GroupCircleList.DataBean> interstList = new ArrayList<>();
    private String groupId = "";
    private int type;
    private int pos;
    private String circleName = "";
    private boolean isCare = false;

    private String circleId = "";
    private String circleUrl = "";

    private String groupName = "";


    @Override
    protected int getContentView() {
        return R.layout.fragment_circle;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);


        publicRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter1 = new GroupCircleAdapter(publicList);
        publicRyc.setAdapter(adapter1);
        View emptyView1 = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter1.setEmptyView(emptyView1);
        adapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent, Group);
                    return;
                }
                if (publicList.get(position).getIs_follow() == 1) {
                    circleName = publicList.get(position).getC_name();
                    circleId = String.valueOf(publicList.get(position).getC_id());
                    circleUrl = publicList.get(position).getHead_url();
                    groupId = publicList.get(position).getGroup_id();
                    if (getActivity() instanceof GroupDetailsActivity) {
                        groupName = ((GroupDetailsActivity) getActivity()).getName();
                    }

                    Intent intent = new Intent(mContext, ChatActivity.class);
                    intent.putExtra("type", 2);
                    intent.putExtra("id", groupId);
                    intent.putExtra("toName", circleName);
                    intent.putExtra("toUrl", circleUrl);
                    intent.putExtra("toId", circleId);
                    intent.putExtra("groupName", groupName);
                    startActivity(intent);
                } else {
                    Toast.makeText(mContext, "您尚未关注该圈子！", Toast.LENGTH_SHORT).show();
                }
            }
        });
        adapter1.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.add_care:
                        if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                            Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(mContext, LoginActivity.class);
                            startActivityForResult(intent, Group);
                            return;
                        }
                        circleName = publicList.get(position).getC_name();
                        circleId = String.valueOf(publicList.get(position).getC_id());
                        circleUrl = publicList.get(position).getHead_url();
                        if (getActivity() instanceof GroupDetailsActivity) {
                            isCare = ((GroupDetailsActivity) getActivity()).isCare();
                        }
                        if (isCare) {
                            loadingDialog.show();
                            pos = position;
                            type = 1;
                            circleName = publicList.get(position).getC_name();
                            groupId = publicList.get(position).getGroup_id();
                            AppApi.applyJoinCircle(publicList.get(position).getC_id(), MyUtils.getCode(mContext), null, GroupCircleFragment.this, 2, getActivity());
                        } else {
                            Toast.makeText(mContext, "您尚未关注改社群，请关注后加入该圈子", Toast.LENGTH_SHORT).show();
                        }

                        break;
                }
            }
        });

        interstRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter2 = new GroupCircleAdapter(interstList);
        interstRyc.setAdapter(adapter2);
        View emptyView2 = getLayoutInflater().inflate(R.layout.empty_view, null);
        adapter2.setEmptyView(emptyView2);
        adapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                    Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivityForResult(intent, Group);
                    return;
                }
                if (interstList.get(position).getIs_follow() == 1) {
                    circleName = interstList.get(position).getC_name();
                    circleId = String.valueOf(interstList.get(position).getC_id());
                    circleUrl = interstList.get(position).getHead_url();
                    groupId = publicList.get(position).getGroup_id();
                    if (getActivity() instanceof GroupDetailsActivity) {
                        groupName = ((GroupDetailsActivity) getActivity()).getName();
                    }

                    Intent intent = new Intent(mContext, ChatActivity.class);
                    intent.putExtra("type", 2);
                    intent.putExtra("id", groupId);
                    intent.putExtra("toName", circleName);
                    intent.putExtra("toUrl", circleUrl);
                    intent.putExtra("toId", circleId);
                    intent.putExtra("groupName", groupName);
                    startActivity(intent);
                } else {
                    Toast.makeText(mContext, "您尚未关注该圈子！", Toast.LENGTH_SHORT).show();
                }

            }
        });
        adapter2.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.add_care:
                        if (MyUtils.getToken(mContext) != null && MyUtils.getToken(mContext).length() == 0) {
                            Toast.makeText(mContext, "您尚未登陆，请先登录！", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(mContext, LoginActivity.class);
                            startActivityForResult(intent, Group);
                            return;
                        }
                        circleName = interstList.get(position).getC_name();
                        circleId = String.valueOf(interstList.get(position).getC_id());
                        circleUrl = interstList.get(position).getHead_url();
                        loadingDialog.show();
                        pos = position;
                        type = 2;
                        groupId = interstList.get(position).getGroup_id();
                        circleName = interstList.get(position).getC_name();
                        AppApi.applyJoinCircle(interstList.get(position).getC_id(), MyUtils.getCode(mContext), null, GroupCircleFragment.this, 2, getActivity());
                        break;
                }
            }
        });

        publicRyc.setHasFixedSize(true);
        publicRyc.setNestedScrollingEnabled(false);

        interstRyc.setHasFixedSize(true);
        interstRyc.setNestedScrollingEnabled(false);


        if (getActivity() instanceof GroupDetailsActivity) {
            id = ((GroupDetailsActivity) getActivity()).getGroupId();
            AppApi.groupCircle(id, this, 0, getActivity());
        }
    }

    public void doRefresh() {
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter1.notifyDataSetChanged();
        adapter2.notifyDataSetChanged();
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 0:
                publicList.clear();
                interstList.clear();
                GroupCircleList groupCircleList = JSON.parseObject(result, GroupCircleList.class);
                List<GroupCircleList.DataBean> dataBeanList = groupCircleList.getData();
                for (GroupCircleList.DataBean dataBean : dataBeanList) {
                    if (dataBean.getType() == 1) {
                        publicList.add(dataBean);
                    } else if (dataBean.getType() == 2) {
                        interstList.add(dataBean);
                    }
                }

                adapter1.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                break;
            case 2:
                ApplyBean applyBean = JSON.parseObject(result, ApplyBean.class);
                if (type == 1) {
                    if (applyBean.getData().getType() == 1) {
                        TextTipDialogFragment textTipDialogFragment = new TextTipDialogFragment();
                        textTipDialogFragment.setContent("您的申请已提交，请耐心等待审核o~");
                        textTipDialogFragment.show(getFragmentManager(),"tip");
                    } else {
                        publicList.get(pos).setIs_follow(1);
                        adapter1.notifyDataSetChanged();
                        Intent intent = new Intent(mContext, ChatActivity.class);
                        intent.putExtra("type", 2);
                        intent.putExtra("id", groupId);
                        intent.putExtra("toName", circleName);
                        intent.putExtra("toUrl", circleUrl);
                        intent.putExtra("toId", circleId);
                        intent.putExtra("groupName", groupName);
                        startActivity(intent);
                    }

                } else if (type == 2) {
                    if (applyBean.getData().getType() == 1) {
                        TextTipDialogFragment textTipDialogFragment = new TextTipDialogFragment();
                        textTipDialogFragment.setContent("您的申请已提交，请耐心等待审核o~");
                        textTipDialogFragment.show(getFragmentManager(),"tip");
                    } else {
                        interstList.get(pos).setIs_follow(1);
                        adapter2.notifyDataSetChanged();
                        Intent intent = new Intent(mContext, ChatActivity.class);
                        intent.putExtra("type", 2);
                        intent.putExtra("id", groupId);
                        intent.putExtra("toName", circleName);
                        intent.putExtra("toUrl", circleUrl);
                        intent.putExtra("toId", circleId);
                        intent.putExtra("groupName", groupName);
                        startActivity(intent);
                    }

                }
                if (getActivity() instanceof GroupDetailsActivity) {
                    groupName = ((GroupDetailsActivity) getActivity()).getName();
                }

                break;
        }

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();

    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();

    }

    @OnClick({R.id.more_public, R.id.more_interst})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.more_public:
                break;
            case R.id.more_interst:
                break;
        }
    }

    public void doRefersh() {
        AppApi.groupCircle(id, this, 0, getActivity());
    }


}
